﻿@*******************************************************************************************************
    //  Settings.cshtml - Gbtc
    //
    //  Copyright © 2016, Grid Protection Alliance.  All Rights Reserved.
    //
    //  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
    //  the NOTICE file distributed with this work for additional information regarding copyright ownership.
    //  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
    //  file except in compliance with the License. You may obtain a copy of the License at:
    //
    //      http://opensource.org/licenses/MIT
    //
    //  Unless agreed to in writing, the subject software distributed under the License is distributed on an
    //  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
    //  License for the specific language governing permissions and limitations.
    //
    //  Code Modification History:
    //  ----------------------------------------------------------------------------------------------------
    //  01/22/2016 - J. Ritchie Carroll
    //       Generated original version of source code.
    //
    //*****************************************************************************************************@
@using GSF.Collections;
@using GSF.Data.Model
@using GSF.Web.Model
@using PQDashboard.Model
@using Newtonsoft.Json
@using Meter = openXDA.Model.Meter
@model AppModel
@{
    Layout = "";
    DataContext dataContext = Model.DataContext;

    List<DashSettings> dashSettings = dataContext.Table<DashSettings>().QueryRecords().ToList();

    List<UserDashSettings> userDashSettings = dataContext.Table<UserDashSettings>().QueryRecords(restriction: new RecordRestriction("UserAccountID IN (SELECT ID FROM UserAccount WHERE Name = {0})", ViewBag.usersid)).ToList();
    int meterCount = dataContext.Table<Meter>().QueryRecordCount(new RecordRestriction("ID IN (SELECT MeterID FROM MeterMeterGroup WHERE MeterGroupID IN (SELECT MeterGroupID FROM UserAccountMeterGroup WHERE UserAccountID =  (SELECT ID FROM UserAccount WHERE Name = {0})))", ViewBag.usersid));
    foreach (DashSettings setting in dashSettings)
    {
        if (setting.Name == "DashTab" || setting.Name == "KML" || setting.Name.EndsWith("Chart"))
        {
            var index = userDashSettings.IndexOf(x => x.Name == setting.Name && x.Value == setting.Value);
            if (index >= 0)
            {
                setting.Enabled = userDashSettings[index].Enabled;
            }
        }
        else if (setting.Name.EndsWith("Colors") )
        {
            var index = userDashSettings.IndexOf(x => x.Name == setting.Name && x.Value.Split(',')[0] == setting.Value.Split(',')[0]);
            if (index >= 0)
            {
                setting.Value = userDashSettings[index].Value;
            }
        }
        else if (setting.Name.Contains("System."))
        {
            var index = userDashSettings.IndexOf(x => x.Name == setting.Name);
            if (index >= 0)
            {
                setting.Value = userDashSettings[index].Value;
            }
        }
    }

    IEnumerable<DashSettings> kml = dashSettings.Where(x => x.Name == "KML");

}
<div id="settingsModal" class="modal fade" role="dialog">
    <div class="modal-dialog modal-lg">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">The Open PQDashboard version 2.0</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#about" data-toggle="tab">About</a></li>
                    <li><a href="#config" data-toggle="tab">Config</a></li>
                    <li><a href="#help" data-toggle="tab">Help</a></li>
                </ul>
                <div class="tab-content">
                    <div id="about" class="tab-pane fade in active">

                        <h3><a href="http://gridprotectionalliance.org/default.asp" target="_blank"><img src="~/Images/ConfigImages/lock.png" style="height:30px" /></a>&nbsp;&nbsp;About the Open PQDashboard</h3>
                        <br />
                        <p>
                            The PQ Dashboard enables the visualization of findings and insights derived from event waveforms and power system trending data that are housed within GPA’s <a href="http://github.com/GridProtectionAlliance/openXDA" target="_blank" style="color:blue;">openXDA solution.</a><br />
                            Development of the PQ Dashboard has been largely funded by the Electric Power Research Institute (EPRI).
                        </p>
                        <p>
                            For information on updates and to log issues and make suggestions for improvements, go to the <a href="http://github.com/GridProtectionAlliance/PQDashboard" target="_blank" style="color:blue;">PQ Dashboard GitHub repository.</a>
                        </p>
                        <p>
                            GPA offers annual product support agreements for the Open PQ Dashboard and openXDA as well as customized services that are tailored to meet the needs of individual utilities. <br />
                            For more information about GPA’s support and maintenance services, please contact us at:
                        </p>
                        <p>
                            1206 Broad Street <br />
                            Chattanooga, TN 34702 <br />
                            Phone: (423) 702-8136 <br />
                            Email: <a href="mailto:info@gridprotectionalliance.org?subject=Contact%20GPA" style="color:blue;">info@gridprotectionalliance.org</a>
                        </p>
                    </div>
                    <div id="config" class="tab-pane fade" style="overflow-y:auto; max-height: 750px">
                        <h3 class="modal-title"><img src="~/Images/ConfigImages/tools.png" style="height:30px" />&nbsp;&nbsp;Settings for @ViewBag.username</h3>
                        <em>Settings are automatically udpated after a change is made.  To see results the browser must be refreshed. Click below to refresh immediately or not.</em>

                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#systemSettings" data-toggle="tab">System</a></li>
                            <li><a href="#tabs" data-toggle="tab">Tabs</a></li>
                            @foreach (DashSettings setting in dashSettings)
                            {
                                if (setting.Name == "DashTab" && setting.Enabled && (new List<string>() { "#tabsMeterActivity", "#tabsOverviewToday", "#tabsOverviewYesterday", "#tabsTrendingData", "#tabsModbusData", "#tabsHistorianData" }).IndexOf(setting.Value) < 0)
                                {
                                    <li><a href="#@setting.Value.Substring(5).ToLower()" data-toggle="tab">@setting.Value.Substring(5)</a></li>
                                }
                            }
                            <li><a href="#kml" data-toggle="tab">KML</a></li>
                        </ul>
                        <div class="tab-content">
                            <div id="systemSettings" class="tab-pane fade in active">
                                <div class="well">
                                    <table class="table table-responsive">
                                        <thead>
                                            <tr>
                                                <th>Setting</th>
                                                <th>Value</th>
                                            </tr>
                                        </thead>
                                        @foreach (DashSettings setting in dashSettings.Where(x => x.Name.Contains("System.")))
                                        {
                                                <tr>
                                                    <td>
                                                        @setting.Name
                                                    </td>
                                                    <td>
                                                        <input type="text" onchange="updateSystemSetting(this, @JsonConvert.SerializeObject(setting))" value="@setting.Value"/>
                                                    </td>
                                                </tr>
                                            
                                        }
                                    </table>
                                </div>

                            </div>
                            <div id="tabs" class="tab-pane fade">
                                <div class="well">
                                    <table class="table table-responsive">
                                        <thead>
                                            <tr>
                                                <th>Tab</th>
                                                <th>Enabled</th>
                                            </tr>
                                        </thead>
                                        @foreach (DashSettings setting in dashSettings.Where(x => x.Name == "DashTab"))
                                        {
                                                <tr>
                                                    <td>
                                                        @setting.Value
                                                    </td>
                                                    <td>
                                                        <input id="tab@(setting.ID)" type="checkbox" onchange="updateTabSetting(@JsonConvert.SerializeObject(setting))" @(setting.Enabled ? "checked='checked'" : "") />
                                                    </td>
                                                </tr>
                                        }
                                    </table>
                                </div>
                            </div>

                            @foreach (DashSettings setting in dashSettings)
                            {
                                if (setting.Name == "DashTab" && setting.Enabled && (new List<string>() { "#tabsMeterActivity", "#tabsOverviewToday", "#tabsOverviewYesterday", "#tabsTrendingData", "#tabsModbusData", "#tabsHistorianData" }).IndexOf(setting.Value) < 0)
                                {
                                    <div id="@setting.Value.Substring(5).ToLower()" class="tab-pane fade">
                                        <div class="well">
                                            <table class="table table-responsive">
                                                <thead>
                                                    <tr>
                                                        <th>Field</th>
                                                        <th>Color</th>
                                                        <th>Enabled</th>
                                                    </tr>
                                                </thead>
                                                @foreach (DashSettings innerSetting in dashSettings)
                                                {
                                                    if (setting.Value.Substring(5) + "Chart" == innerSetting.Name)
                                                    {
                                                        <tr>
                                                            <td>
                                                                @innerSetting.Value
                                                            </td>
                                                            <td>
                                                                @{
                                                                    DashSettings color = dashSettings.Where(x => x.Name.Contains(setting.Value.Substring(5) + "ChartColors") && string.Compare(innerSetting.Value, x.Value.Split(',')[0]) == 0)?.FirstOrDefault();
                                                                }
                                                                <input id="color@(color.ID)" style="background-color: @(color?.Value.Split(',')?[1])" onchange="updateColorSetting(@JsonConvert.SerializeObject(color), this)" value="@(color?.Value.Split(',')?[1] ?? "")" />
                                                            </td>
                                                            <td>
                                                                <input id="enable@(innerSetting.ID)" onchange="updateEnableSetting(@JsonConvert.SerializeObject(innerSetting))" type="checkbox" @(innerSetting.Enabled ? "checked='checked'" : "") />
                                                            </td>
                                                        </tr>
                                                                        }
                                                                    }
                                            </table>
                                        </div>
                                    </div>
                                                                        }
                                                                    }

                            <div id="kml" class="tab-pane fade">
                                <div class="well">
                                    <table class="table table-responsive">
                                        <thead>
                                            <tr>
                                                <th>File</th>
                                                <th>Enabled</th>
                                            </tr>
                                        </thead>
                                        @foreach (DashSettings setting in kml)
                                        {
                                            <tr>
                                                <td>
                                                    @setting.Value
                                                </td>
                                                <td>
                                                    <input id="tab@(setting.ID)" type="checkbox" onchange="updateKMLSetting(@JsonConvert.SerializeObject(setting))" @(setting.Enabled ? "checked='checked'" : "") />
                                                </td>
                                            </tr>
                                        }
                                    </table>

                                </div>
                            </div>

                        </div>
                    </div>
                    <div id="help" class="tab-pane fade" style="overflow-y:auto; max-height: 750px">
                        <h3 class="modal-title"><img src="~/Images/ConfigImages/qmark.png" style="height:30px" />&nbsp;&nbsp;PQDashboard Help</h3>
                        <br />

                        <h4><b>How to configure the dashboard</b></h4>

                        <br /><br />
                        <table class="table-bordered">
                            <tr> <th colspan="2">Description of configuration settings</th> </tr>
                            <tr>
                                <td width="20%"><b>Tabs</b></td>
                                <td>Hide or show available tabs</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Correctness</b></td>
                                <td>Configure the color and visibility of available correctness levels</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Events</b></td>
                                <td>Configure the color and visibility of different event types.</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Completeness</b></td>
                                <td>Configure the color and visibility of available completeness levels</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Trending</b></td>
                                <td>Configure the color and visibility of trending data types</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Breakers</b></td>
                                <td>Configure the color and visibility of different breaker event types</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Disturbances</b></td>
                                <td>Configure the color and visibility of different types of disturbances</td>
                            </tr>
                            <tr>
                                <td width="20%"><b>Faults</b></td>
                                <td>Configure the color and visibility of different types of events</td>
                            </tr>
                        </table>

                        <br /><br />
                        @*<h4><b>FAQs</b></h4>
                        <p>
                            <b>What if I need more help than I can find online?</b><br />
                            &nbsp;&nbsp; You can always ask a question in the <a href="http://discussions.gridprotectionalliance.org/" target="_blank" style="color: blue;">GPA user forum.</a>
                        </p>
                        <p>
                            <b>What’s the difference between the “events” and “faults” tab</b> <br />
                            &nbsp;&nbsp; Not all events are faults. Only faults show up in the faults tab, all events show up in the events tab.
                        </p>
                        <p>
                            <b>Why do I sometimes see the same event more than once?</b><br />
                            &nbsp;&nbsp; Not sure.
                        </p>
                        <p>
                            <b>Why can’t I see trending data?</b><br />
                            &nbsp;&nbsp; Not sure. Maybe you don’t have trending data?
                        </p>
                        <p>
                            <b>Why do most events seem to be classified as a “swell”?</b><br />
                            &nbsp;&nbsp; That’s the default way of classifying things.
                        </p>
                        <p>
                            <b>Why are there no events for some sites?</b><br />
                            &nbsp;&nbsp; Maybe you need to pick a different time range to see events for it??
                        </p>
                        <p>
                            <b>Do I need to do anything to configure the PQ Dashboard as new monitors are added to openXDA?</b><br />
                            &nbsp;&nbsp; I don’t think so, not sure though???
                        </p>
                        <p>
                            <b>How can I add new charts and displays to the dashboard?</b><br />
                            &nbsp;&nbsp; PQDashboard is open source and is <a href="https://github.com/GridProtectionAlliance/PQDashboard" target="_blank" style="color:blue;">posted on GitHub</a>, you are welcome to add more things there. <br />
                            &nbsp;&nbsp; Please follow the guidelines in the <a href="https://www.gridprotectionalliance.org/docs/GPA_Coding_Guidelines_2011_03.pdf" target="_blank" style="color:blue;">contributing guide.</a>
                        </p>
                        <p>
                            <b>Why do some waveforms identified as “faults” in the COMTRADE file not show up as faults in the dashboard?</b><br />
                            &nbsp;&nbsp; Because PQDashboard/openXDA implements its own logic for classifying faults, which may be different than your specific DFR.
                        </p>*@


                    </div>

                </div>

            </div>
            <div class="modal-footer">
                <button type="button" id="configApply" class="btn btn-primary" data-dismiss="modal" onclick="refresh()">Apply Now</button>
                <button type="button" id="configDefaults" class="btn btn-primary" onclick="resetDefaults()">Restore Defaults</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

<script>
    var dashSettings = JSON.parse('@Html.Raw(JsonConvert.SerializeObject(dashSettings))');
    var kmlData = @Html.Raw(JsonConvert.SerializeObject(kml.Where(x =>x.Enabled).FirstOrDefault()));

    function refresh() {
        location.reload();
    }

    function resetDefaults() {
        dataHub.resetDefaultSettings().done(function () { refresh(); });
    }

    function updateTabSetting(setting) {
        dataHub.updateDashSettings(setting.ID, setting.Name, setting.Value, !setting.Enabled, userId)
    }
  
    function updateSystemSetting(input, setting) {
        setting.Value = input.value;
        dataHub.updateDashSettings(setting.ID, setting.Name, setting.Value, setting.Enabled, userId)
    }

    function updateColorSetting(setting, element) {
        $(element).css('background-color', element.value);
        dataHub.updateDashSettings(setting.ID, setting.Name, setting.Value.split('#')[0] + element.value, !setting.Enabled, userId)
    }

    function updateEnableSetting(setting) {
        dataHub.updateDashSettings(setting.ID, setting.Name, setting.Value, !setting.Enabled, userId)
    }

    function updateKMLSetting(setting) {
        dataHub.updateDashSettings(setting.ID, setting.Name, setting.Value, !setting.Enabled, userId)
    }

</script>